import tkinter
from tkinter import Tk, filedialog, Button, messagebox, Label
from word_tool import add_img
from pdf_tool import convert_to_img
import os, time


# 转换事件
def event_convert():
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
    if file_path:
        prefix = file_path[file_path.rfind('/'): file_path.rfind('.')]
        save_path = file_path[0: file_path.rfind('/')] + prefix + '_' + str(int(time.time()))
        # 创建目录
        os.makedirs(save_path)
        # 处理
        try:
            convert_to_img(file_path, save_path, prefix)
        except:
            messagebox.showerror(title='结果', message="处理失败")
        # 转化为 word
        images = os.listdir(save_path)
        for i in range(0, len(images)):
            images[i] = save_path + '/' + images[i]
        add_img(save_path + prefix, images, 15)
        messagebox.showinfo(title='结果', message="处理完成，图片已保存在 PDF 文件所在位置")


window = Tk()
# 标题
window.title("ITmake-PDF 转图片")
# 窗口大小并设定居中
width = 300
height = 300
window.geometry('%dx%d+%d+%d' %
                (width, height,(window.winfo_screenwidth() - width) / 2, (window.winfo_screenheight() - height) / 2))
# 不可拉伸
window.resizable(False, False)
# 文字
info_text = Label(window, text="        v1.0.0        ", bg="white", fg="gray",
                font=('Times', 10, 'bold italic underline')).pack()
# 按钮
convert_btn = Button(window, text="请选择需要转换为图片的 PDF 文件", command=event_convert, width=30, height=2).pack()
# 显示主窗口
window.mainloop()

# pyinstaller -F -w -n PDF_转换器 winform.py -p pdf_tool.py -p word_tool.py
